
import java.awt.Color;
import java.awt.Font;
import java.awt.Polygon;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Random;
import java.util.TreeSet;

import pluginUtils.AGCircle;
import pluginUtils.Complexe;
import pluginUtils.Point2D;

import ij.ImagePlus;
import ij.plugin.PlugIn;
import ij.process.ColorProcessor;
import ij.process.ImageProcessor;
import laplacianGrowthUtils.*;


public class LaplacianGrowth extends ImagePlus implements PlugIn{
	
{Locale.setDefault(Locale.US); }
	
	private ImageProcessor primeIP; 
	private int X=2000,Y=2000; // image size
	
	
	private double xShift=0; 
	private double zShift=0; 
	

	private Random generator=new Random(); // One always needs some randomness
	
	// For the scaling of the image
	private double x0=0,x1=1,y0=0,y1=1;
	private double acoef,ccoef,vcoef,wcoef; 
	

	public void setBounds(double xl,double xu,double yl,double yu){
		x0=xl; 
		x1=xu;
		y0=yl;
		y1=yu; 
	}
	
	private void drawPoint(double x,double y){
		int radi=1; 
		int intX=(int)(x*acoef+ccoef);
		int intY=(int)(y*vcoef+wcoef);
		primeIP.fillOval(intX-radi,intY-radi,2*radi,2*radi);
	}
	

	private void drawLine(double xd,double yd,double xf,double yf){
		int XD=(int)(xd*acoef+ccoef); 
		int YD=(int)(yd*vcoef+wcoef); 
		int XF=(int)(xf*acoef+ccoef); 
		int YF=(int)(yf*vcoef+wcoef);
		primeIP.drawLine(XD,YD,XF,YF); 
	}
	
	
	private void drawLine(Point2D p,Point2D q){
		int XD=(int)(p.getX()*acoef+ccoef); 
		int YD=(int)(p.getY()*vcoef+wcoef); 
		int XF=(int)(q.getX()*acoef+ccoef); 
		int YF=(int)(q.getY()*vcoef+wcoef);
		primeIP.drawLine(XD,YD,XF,YF); 
	}
	
	
	public void setCoefs(){
		acoef=X/(x1-x0); 
		vcoef=Y/(y1-y0); 
		ccoef=-acoef*x0; 
		wcoef=-vcoef*y0; 
	}
	 

	public void run(String arg) {
		primeIP = new ColorProcessor(X,Y);
		
		
		
	
		double limit=2000; 
		double lmoins=0;
		
		
		setBounds(lmoins,limit,lmoins,limit);
		//setBounds(-0.25,0.5,-0.25,0.5); 
		setCoefs(); 
	
		Polygon background=new Polygon(new int[]{0,0,X,Y},new int[]{0,X,Y,0},4);
		primeIP.setColor(Color.WHITE);
		
		primeIP.fillPolygon(background);
		
		
		primeIP.setFont(new Font(null,Font.ITALIC,60));
		primeIP.setColor(Color.BLACK);
		primeIP.setLineWidth(1); 
		

		setProcessor("Verifying", primeIP);
		
	
		primeIP.setColor(Color.BLACK);
		primeIP.setLineWidth(4);
	
		primeIP.setColor(Color.BLACK);
		
		// initialisation
		// TODO : correction au pif...
		ListeCandidats2D lc=new ListeCandidats2D(5, 2); 
		lc.setBounds(lmoins,limit); 
		
		
		// Ajouter le premier point
		int midx=(int)((limit-lmoins)/2); 
		int midy=(int)((limit-lmoins)/2); 
		Grid2DPotential p=new Grid2DPotential(midx,midy); 
		lc.addSeed(p);
		
		
		show(); 
		for(int i=1;i<10000000;i++){
		
			if(i%10000==0){
				updateAndDraw(); 
				System.out.println(i);
			}
			Grid2DPotential n=lc.roulette(); 
			//System.out.println(i+" "+n); 
			int xn=n.getX(); 
			int yn=n.getY(); 
			drawPoint(xn,yn);	
		}//i
				
		
		
		show(); 
		System.out.println("fini");
	}	
	
	
	

}
